package com.weishuang.monitor.leader.config.security.handler;

import cn.hutool.json.JSONUtil;
import com.weishuang.monitor.leader.commons.entity.Result;
import com.weishuang.monitor.leader.commons.utils.ResultUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        Result info = ResultUtils.info(e.getMessage());
        httpServletResponse.setStatus(200);
        httpServletResponse.setHeader("Content-type","application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(JSONUtil.toJsonPrettyStr(info));
    }
}
